package com.qf.rabbit.work;

import com.qf.rabbit.utils.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.lang.annotation.Target;


public class Consumer1 {
    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        //生产者和消费者声明队列的时候必须使用一模一样的参数, 有一个不一样都不行
        channel.queueDeclare("bk2105", true, false, false, null);
        channel.basicQos(1);
        //消费消息, autoack 自动应答
        channel.basicConsume("bk2105", false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.err.println("消费者 11111 收到了:"+new String(body));
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });

    }
}
